package mysql

import (
	"fresh-box/freshrpc/freshrpc"

	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	Name       string  `gorm:"type:varchar(30);not null;comment:'名称'"`
	Price      float64 `gorm:"type:decimal(10,2);not null;comment:'价格'"`
	Image      string  `gorm:"type:varchar(500);not null;comment:'图片'"`
	CategoryId int64   `gorm:"type:int;not null;comment:'分类ID'"`
}
type Category struct {
	gorm.Model
	Name string `gorm:"type:varchar(30);not null;comment:'分类名称'"`
}

func (c *Category) CategoryCreate(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (f *Fresh) FreshCreate(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Fresh) FreshList(db *gorm.DB, in *freshrpc.FreshListRequest, lists []*freshrpc.FreshList) ([]*freshrpc.FreshList, error) {
	err2 := db.Debug().Model(&Fresh{}).
		Select("fresh.id as fresh_id", "name", "price", "image", "category.name as category_name").Scopes(Paginate(int(in.Page), int(in.Size))).Error
	return lists, err2
}
